import orderApi from '../../api/order'
const { BASEURL } = require('../../request/config')
Page({
  data: {
    baseUrl:BASEURL,
    selectType: 'all',
    list:[],
    current:1,
    total:0,
    pages:0,//总页数
    loading:false,
    moreLoading:false,//上拉加载
    allFlg:false,//上拉到底了
    parameterList:[
      {
        label: ""
       },
     {
      label: "球镜"
     },
      {
      label: "柱镜"
     },{
      label: "轴位"
     },{
      label: "ADD"
     },{
      label: "瞳距"
     },{
      label: "数量"
     }
    ],
    parameterRValue:[
      "R",
      "12.22",
      "1",
      "2",
      "3",
      "4",
      '5'
    ],
    parameterLValue:[
      "L",
      "12.22",
      "1",
      "2",
      "3",
      "4",
      '5'
    ],
    pageSize:10,
    orderMap:{
      "-1":"已取消",
      "0":"待付款",
      "1":"待发货",
      "2":"待收货",
      "3":"已完成",
      "4":"付款成功",
      "5":"待取货"
    }
  },
  onLoad() {
    this.orderPage()
  },
  selectBtnType(data) {
    let  type = data.target.dataset.index
    console.log("type------>",type)
    this.setData({
      "selectType":type,
    })
    this.onPullDownRefresh()
  },
  onReachBottom() {
    console.log("this.data.loading------xx-->",this.data.loading,this.data.current)
    if (!this.data.loading&&this.data.current < this.data.pages) {
      let current = this.data.current
      current = current + 1
      this.setData({
        current,
        moreLoading:true
      })
      this.orderPage(false)
    }else {
      this.setData({
        allFlg:true
      })
    }
  },
  onPullDownRefresh() {
      // 显示顶部刷新图标  
      wx.showNavigationBarLoading();
      this.setData({
        loading:true,
        current:1,
        allFlg:false
      })
      this.orderPage(true)
  },
  orderPage(flg) {
    let typEenum = {
      "all": null,
      'deliver':1,
      "payment":0,
      'done':3
    }     
    let data = {
      status:typEenum[this.data.selectType],
      pageSize:this.data.pageSize,
      pageNum:this.data.current
    }
    orderApi.orderPage(data).then((res)=>{
      let records = res.records
      //下拉
      if(flg) {
        this.setData({
          loading:false,
        })
      }else {
        //上拉
        records = this.data.list.concat(...records)
        this.setData({
          moreLoading:false,
        })
      }
      this.setData({
        list:records,
        total:res.total,
        pages:res.pages,
        allFlg:false
      })

      // 隐藏导航栏加载框  
      wx.hideNavigationBarLoading();
      // 停止下拉动作  
      wx.stopPullDownRefresh();
        console.log("orderPage----xx-->",res)
    })
  },
  onClick(e) {
    let item = e.currentTarget.dataset.item
    let orderId = item.orderId
    wx.navigateTo({
      url: '../orderDetails/orderDetails?id='+orderId
    })
  },
   /**
  * 生命周期函数--监听页面显示
  */
   onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
  }
})